/*
 * Filename:    WarnDataServiceImpl.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年11月5日
 */
package com.paic.mhis.hcpms.quota.calculationreport.calculationjob.biz.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.biz.service.IWarningDataService;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.biz.service.KpiJobCtrlService;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.constants.CalculationJobConstants;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.dto.HcpmsKpiJobCtrlDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.dto.KpiBatchDetailDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.dto.KpiBatchJobLogDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.dto.KpiBatchParamterDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.intergration.dao.KpiBatchDao;
import com.paic.mhis.hcpms.warning.biz.service.WarningInfoJobService;
import com.paic.mhis.hcpms.warning.biz.service.WarningPoolService;
import com.paic.mhis.hcpms.warning.dto.WarningParameterJobDTO;
import com.pingan.um.client.util.UUID;

@Service("warningDataServiceImpl")
public class WarningDataServiceImpl implements IWarningDataService {
	public static Log log = LogFactory.getLog(WarningDataServiceImpl.class);

	@Autowired
	private KpiBatchDao kpiBatchDao;
	@Autowired
	private WarningPoolService warnPoolService;
	@Autowired
	private WarningInfoJobService warnInfoService;
	@Autowired
	private KpiJobCtrlService kpiJobCtrlService;

	public void processWarningData() {
		log.info("-----预警处理job进入-----");
		HcpmsKpiJobCtrlDTO ctrlDto = new HcpmsKpiJobCtrlDTO();
		ctrlDto.setJobCode("J20141217004");
		ctrlDto.setJobName("预警事件生成JOB");
		ctrlDto.setJobStatus(CalculationJobConstants.CTRL_JOB_STATUS_USE);
		ctrlDto.setTmpId(UUID.getID());
		ctrlDto = kpiJobCtrlService.getHcpmsJobCtrl(ctrlDto);
		// if (ctrlDto != null) {
		//
		// ctrlDto.setJobStatus(CalculationJobConstants.CTRL_JOB_STATUS_USE);
		// kpiJobCtrlService.updateHcpmsJobCtrl(ctrlDto);
		// }
		if (ctrlDto == null) {
			log.info("-----没有数据进行指标计算或者JOB被占用-----");
			return;
		}
		// String uuid = UUID.getID();
		//
		// this.kpiBatchDao.updateKpiBatchDetailDTOTempBatchByStatus(uuid,
		// CalculationJobConstants.KPI_BATCH_DETAIL_04);

		KpiBatchParamterDTO paramter = new KpiBatchParamterDTO();
		paramter.setStatus(CalculationJobConstants.KPI_BATCH_DETAIL_04);
		// paramter.setTempBatch(uuid);
		List<KpiBatchDetailDTO> list = this.kpiBatchDao.queryKpiBatchDetailDTO(paramter);

		if (null != list && list.size() > 0) {
			for (KpiBatchDetailDTO bean : list) {
				String id = bean.getIdKpiBatchDetail();
				String areCode = bean.getRegionCode();
				String month = bean.getMonth();

				// 更新当前记录为预警处理开始
				this.kpiBatchDao.updateKpiBatchDetailDTOStatus(id, CalculationJobConstants.KPI_BATCH_DETAIL_05);

				// 预警处理开始
				log.info("-------预警事件池处理开始-------");
				WarningParameterJobDTO warningParameterJobDTO = new WarningParameterJobDTO();
				warningParameterJobDTO.setRegionCode(areCode);
				warningParameterJobDTO.setKpiTime(month);
				KpiBatchJobLogDTO logDto = new KpiBatchJobLogDTO();
				logDto.setBatchNo(bean.getBatchNo());
				logDto.setCreateBy(bean.getCreatedBy());
				logDto.setUpdateBy(bean.getUpdatedBy());
				logDto.setCreateDate(new Date());
				logDto.setJobName(ctrlDto.getJobName());
				logDto.setJobNameDesc(KpiDataItemServiceImpl.class.getName());
				warnPoolService.insertWarnPoolData(warningParameterJobDTO);
				log.info("-------预警事件池处理结束-------");
				log.info("-------预警任务处理开始-------");
				warnInfoService.insertWarnInfo(warningParameterJobDTO);
				log.info("-------预警任务处理结束-------");
				logDto.setUpdateDate(new Date());
				logDto.setOpertionTime(new Date());
				logDto.setSuccessMessage("指标生成预警事件完成", areCode, month);
				logDto.setStatus(CalculationJobConstants.status_01);
				kpiJobCtrlService.insertKpiJobLog(logDto);
				// 更新当前记录为预警处理完成
				this.kpiBatchDao.updateBatchDetailTempBatchIsNull(CalculationJobConstants.KPI_BATCH_DETAIL_06, id);
			}
		} else {
			log.info("-----没有数据进行指标计算-----");
		}
		ctrlDto.setJobStatus(CalculationJobConstants.CTRL_JOB_STATUS_IDLE);
		ctrlDto.setTmpId(null);
		kpiJobCtrlService.updateHcpmsJobCtrl(ctrlDto);
	}
}
